package net.zjitc.action.Admin.Category;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import net.zjitc.damain.category.Category;
import net.zjitc.service.Category.CategoryException;
import net.zjitc.service.Category.CategoryService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.List;
import javax.annotation.Resource;

/**
 * Created by Administrator on 2017/3/13.
 */
@Controller
@Scope("prototype")
public class CategoryAction extends ActionSupport implements ModelDriven<Category> {
    @Resource
    private CategoryService categoryService;

    private Category Model = new Category();

    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    private Integer cid;

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public String add(){
        categoryService.add(Model);
        return "add";
    }

    public String findAll(){
        List<Category> categoryList = categoryService.findAll();
        ActionContext.getContext().put("categoryList",categoryList);
        return "findAll";
    }
    public String delete(){
        Category category = categoryService.findById(cid);
        try {
            categoryService.Delete(category);
        } catch (CategoryException e) {
            e.printStackTrace();
            message = e.getMessage();
            return "deleteError";
        }
        return "delete";
    }
    @Override
    public Category getModel() {
        return Model;
    }
}
